package xworker.dataObject.db.dialets;

import xworker.dataObject.PageInfo;
import xworker.dataObject.db.SqlDialet;
import xworker.dataObject.query.ConditionValue;
import xworker.dataObject.utils.PageType;

import java.util.List;

public class PostgreSqlDialet implements SqlDialet {
    @Override
    public String getCountSql(String sql) {
        return "select count(1) from (" + sql + ") as t";
    }

    @Override
    public String getPageSql(String sql, List<ConditionValue> valueList, PageInfo pageInfo) {
        if(pageInfo != null && pageInfo.getLimit() > 0) {
            sql = sql + " LIMIT ? OFFSET ?";

            long start = pageInfo.getStart() ;
            long limit = pageInfo.getLimit();
            valueList.add(new ConditionValue(limit, "long"));
            valueList.add(new ConditionValue(start, "long"));
        }

        return sql;
    }

    @Override
    public PageType getPageType() {
        return PageType.LIMIT;
    }
}
